﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Sms
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Sms;
    using LdCms.IDAL.Sms;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using System.Linq.Expressions;

    public partial class TencentSendRecordService:BaseService<Ld_Sms_TencentSendRecord>,ITencentSendRecordService
    {
        private readonly ITencentSendRecordDAL TencentSendRecordDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public TencentSendRecordService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, ITencentSendRecordDAL TencentSendRecordDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.TencentSendRecordDAL = TencentSendRecordDAL;
            this.Dal = TencentSendRecordDAL;
        }
        public override void SetDal()
        {
            Dal = TencentSendRecordDAL;
        }

        public bool SaveTencentSendRecord(Ld_Sms_TencentSendRecord entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string platformId = entity.PlatformID;
                string sendId = entity.SendID;
                if (string.IsNullOrEmpty(sendId))
                {
                    var primarykey = PrimaryKeyHelper.PrimaryKeyType.SmsTencentSendRecord;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V2;
                    entity.SendID = PrimaryKeyHelper.MakePrimaryKey(primarykey, primaryKeyLen);
                }
                entity.CreateDate = DateTime.Now;
                return Add(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteTencentSendRecord(int systemId, string companyId, string platformId, string sendId)
        {
            try
            {
                return Delete(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId && m.SendID == sendId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sms_TencentSendRecord GetTencentSendRecord(int systemId, string companyId, string platformId, string sendId)
        {
            try
            {
                return Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId && m.SendID == sendId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_TencentSendRecord> GetTencentSendRecordTop(int systemId, string companyId, int count)
        {
            try
            {
                int total = Utility.ToTopTotal(count);
                var expression = ExtLinq.True<Ld_Sms_TencentSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                var expressionScalarLambda = GetExpressionScalarLambda();
                return FindListTop(expression, expressionScalarLambda, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_TencentSendRecord> GetTencentRecordPaging(int systemId, string companyId, int pageId, int pageSize)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Sms_TencentSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                var expressionScalarLambda = GetExpressionScalarLambda();
                int pageIndex = Utility.ToPageIndex(pageId);
                int pageCount = Utility.ToPageCount(pageSize);
                return FindListPaging(expression, expressionScalarLambda, m => m.CreateDate, false, pageIndex, pageCount).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_TencentSendRecord> SearchTencentSendRecord(int systemId, string companyId, string startTime, string endTime, string state, string keyword, int count)
        {
            try
            {
                DateTime dateStartTime = Utility.ToStartTime(startTime);
                DateTime dateEndTime = Utility.ToEndTime(endTime);
                bool isResult = state.ToBool();
                var expression = ExtLinq.True<Ld_Sms_TencentSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId
                && m.CreateDate.Value.Date >= dateStartTime.Date && m.CreateDate.Value.Date <= dateEndTime.Date
                && (string.IsNullOrEmpty(state) ? true : m.IsResult.Value == isResult)
                && m.Mobile.Contains(keyword));
                var expressionScalarLambda = GetExpressionScalarLambda();
                int total = Utility.ToTopTotal(count);
                return FindListTop(expression, expressionScalarLambda, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public int CountTencentSendRecord(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public int CountTencentSendRecord(int systemId, string companyId, string startTime, string endTime, string state, string keyword)
        {
            try
            {
                DateTime dateStartTime = Utility.ToStartTime(startTime);
                DateTime dateEndTime = Utility.ToEndTime(endTime);
                bool isResult = state.ToBool();
                var expression = ExtLinq.True<Ld_Sms_TencentSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId
                && m.CreateDate.Value.Date >= dateStartTime.Date && m.CreateDate.Value.Date <= dateEndTime.Date
                && (string.IsNullOrEmpty(state) ? true : m.IsResult.Value == isResult)
                && m.Mobile.Contains(keyword));
                return Count(expression);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool VerifyTencentSendCode(int systemId, string companyId, string platformId, string sendId, string phone, string code)
        {
            try
            {
                return IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId && m.SendID == sendId && m.Mobile == phone && m.Code == code);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private Expression<Func<Ld_Sms_TencentSendRecord, Ld_Sms_TencentSendRecord>> GetExpressionScalarLambda()
        {
            try
            {
                Expression<Func<Ld_Sms_TencentSendRecord, Ld_Sms_TencentSendRecord>> scalarLambda = m => new Ld_Sms_TencentSendRecord
                {
                    SystemID = m.SystemID,
                    CompanyID = m.CompanyID,
                    PlatformID = m.PlatformID,
                    SendID = m.SendID,
                    Mobile = m.Mobile,
                    Content = m.Content,
                    Description = m.Description,
                    TplID = m.TplID,
                    ErrMsg = m.ErrMsg,
                    Ext = m.Ext,
                    Extend = m.Extend,
                    Fee = m.Fee,
                    NationCode = m.NationCode,
                    Params = m.Params,
                    ReportStatus = m.ReportStatus,
                    SId = m.SId,
                    Sign = m.Sign,
                    Time = m.Time,
                    UserReceiveTime = m.UserReceiveTime,
                    IsResult = m.IsResult,
                    CreateDate = m.CreateDate
                };
                return scalarLambda;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}
